﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using ERP.Studio.Generator;
using MainApp.Modules.MainMenu;
using Core;
using Core.Base;
using System.Runtime.InteropServices;
using ERP.BusinessEntities;
using ERP.DAL;
using ERP.Studio.Generator;

namespace MainApp
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            AppContext.MainAppForm = this;
            Init();

            //this.menuStrip.BackColor = Color.FromArgb(32, 85, 135);
            //this.menuStrip.ForeColor = Color.White;
            //this.menuStrip.Drop

            foreach (Control c in this.Controls)
            {
                if (c is MdiClient)
                    //c.BackColor = Color.FromArgb(112, 146, 191);
                    c.BackColor = Color.White;
            }
        }

        public void Init()
        {
            //AppContext.CurrentUserInfo = new BSUsersInfo();
            AppContext.LoadCurrentCompanyInfo();
            this.ServerLabel.Text = String.Format(@"{0}", Host.db.ConnectionString);
            this.UserLabel.Text = String.Format(@"USER: {0}", AppContext.CurrentUserInfo.No);

        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            SendMessage(this.ActiveMdiChild.Handle, msg.Msg, msg.WParam, msg.LParam);
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK) 
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK) 
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void generatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new BusinessFactoryForm().ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            AppContext.InitModuleList();

            DataSet ds = AppContext.StartUpModuleCtrl.GetAllObjects();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                BSStartupModulesInfo startupModule = (BSStartupModulesInfo)AppContext.StartUpModuleCtrl.GetObjectFromDataRow(dr);

                if (startupModule != null)
                {
                    String strModuleName = startupModule.ModuleName;
                    BaseModule module = AppContext.CreateModule(strModuleName);
                    if (module != null)
                    {
                        module.Open(AppContext.MainAppForm);
                        module.scrStartScreen.Dock = DockStyle.Left;
                    }

                    //if (strModuleName.Contains("Dashboard"))
                        
                }

            }

            //BaseModule module = new MainMenuModule();
            //module.scrStartScreen.TopMost = false;
            ////module.scrStartScreen.Dock = DockStyle.Left;
            ////ToolStripC Controls.Add(module.scrStartScreen);
            ////module.scrStartScreen.Show();
            //module.Open(AppContext.MainAppForm);
            ////module.scrStartScreen.Dock = DockStyle.Left;
            ////module.scrStartScreen.WindowState = FormWindowState.Maximized;
            ////module.scrStartScreen.WindowState = FormWindowState.Maximized;
        }
    }
}
